---
title: "Harvard data"
output: html_document
date: "2025-04-07"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
rm(list = ls())
cat("\014")
```


```{r}
library(vroom)
library(tidyverse)
library(haven)
library(ggthemes)

options(scipen = 999)
```


*2022 Cooperative Election Study Data*

```{r}
data <- vroom("CCES22_Common_OUTPUT_vv_topost.csv")
# 2022 data

```

```{r}
data <- data %>%
  dplyr::select(caseid, commonpostweight, vvweight_post, 
                inputstate, inputzip, lookupzip, CC22_333b, birthyr, educ, gender4, race, 
                votereg, pid3, pid7, ideo5, urbancity, CC22_443_5, CC22_423, CC22_424,
                CC22_333b, CC22_350h, CC22_355c,
                CC22_443_1, CC22_443_2, CC22_443_3, CC22_443_4, CC22_443_5,
                CC22_320d, CC22_320e, CC22_320f, CC22_320g, CC22_320h, pew_religimp,
                CC22_443_1, CC22_443_2, CC22_443_3, CC22_443_4, CC22_443_5,
                CC22_333, CC22_333a, CC22_333c, CC22_333d, CC22_333e) %>%
  #filter(!is.na(inputzip)) %>%
  rename(distrust_federal = CC22_423, distrust_state = CC22_424) %>%
  mutate(zip = as.numeric(lookupzip))
```


*2022 ZIP code to county crosswalk from HUD’s Office of Policy Development and Research (PD&R)*

```{r}
zips <- vroom("zip_county_062022.csv")

zips <- zips %>%
  filter(TOT_RATIO > 0.9) %>%
  mutate(zip = as.numeric(ZIP))

test <- left_join(data, zips, by = "zip")

test <- test %>%
  filter(!is.na(COUNTY)) %>%
  mutate(cty_fips = as.numeric(COUNTY))

# Merge with 2020 Rurality Measure

irr <- vroom("IRR_2000_2020.csv")

irr <- irr %>%
  rename(cty_fips = FIPS2020)

df <- left_join(test, irr, by = "cty_fips")
```


*Recode the trust variable**

```{r}
df <- df %>%
  mutate(trust_federal = case_when(distrust_federal == 1 ~ 4,
                                   distrust_federal == 2 ~ 3, 
                                   distrust_federal == 3 ~ 2, 
                                   distrust_federal == 8 ~ 1)) %>%
  mutate(trust_state = case_when(distrust_state == 1 ~ 4, 
                                 distrust_state == 2 ~ 3, 
                                 distrust_state == 3 ~ 2, 
                                 distrust_state == 8 ~ 1))

```

*Main DV: Support for State Use of RE* 

**Do you support or oppose each of the following proposals? Require that each state use a minimum amount of renewable fuels (wind, solar, and hydroelectric) in the generation of electricity even if electricity prices increase a little?**

*The question only asks for support or opposition -- so binary*
```{r}
df <- df %>%
  mutate(state_rps_support = case_when(CC22_333b == 2 ~ 0,
                                       CC22_333b == 1 ~ 1))

```


*Climate Action Approval Score*

CC22_333	Climate change	From what you know about global climate change or global warming, which of the following statement comes closest to your opinion? <1> Global climate change has been established as a serious problem, and immediate action is necessary <2> There is enough evidence that climate change is taking place and some action should be taken <3> We don't know enough about global climate change, and more research is necessary before we take any actions <4> Concern about global climate change is exaggerated. No action is necessary <5> Global climate change is not occurring; this is not a real issue

CC22_333a	Climate change	Do you support or oppose each of the following proposals? Give the Environmental Protection Agency power to regulate Carbon Dioxide emissions

CC22_333c	Climate change	Do you support or oppose each of the following proposals? Strengthen the Environmental Protection Agency enforcement of the Clean Air Act and Clean Water Act even if it costs U.S. jobs

CC22_333d	Climate change	Do you support or oppose each of the following proposals? Raise the average fuel efficiency for all cars and trucks in the US from 40 miles per gallon to 54.5 miles per gallon by 2025

CC22_333e	Climate change	Do you support or oppose each of the following proposals? Increase fossil fuel production in the U.S. and boost exports of U.S. liquefied natural gas


```{r}

df <- df %>%
  mutate(epa_reg = case_when(CC22_333a == 2 ~ 0,
                             CC22_333a == 1 ~ 1)) %>%
  mutate(cacw_reg = case_when(CC22_333c == 2 ~ 0,
                             CC22_333c == 1 ~ 1)) %>%
  mutate(cafe_reg = case_when(CC22_333d == 2 ~ 0,
                             CC22_333d == 1 ~ 1)) %>%
  mutate(ff_support = case_when(CC22_333d == 2 ~ 1,
                             CC22_333c == 1 ~ 0)) %>%
  mutate(climate_action = case_when(CC22_333 == 1 ~ 5,
                             CC22_333 == 2 ~ 4,
                             CC22_333 == 3 ~ 3,
                             CC22_333 == 4 ~ 2,
                             CC22_333 == 5 ~ 1,))


```

*Create Combined Climate Action Index using PCA*

```{r}
index_data <- df %>% dplyr::select(climate_action, epa_reg, cacw_reg, cafe_reg, 
                            ff_support, state_rps_support)

index_scaled <- scale(index_data)

complete_rows <- complete.cases(index_scaled)
index_complete <- index_scaled[complete_rows, ]

pca <- prcomp(index_complete, center = TRUE, scale. = FALSE)

df$index <- NA

df$index[which(complete_rows)] <- pca$x[, 1]

df$index <- -1 * df$index

# [0, 1] scale
df$index_01 <- (df$index - min(df$index, na.rm = TRUE)) / 
               (max(df$index, na.rm = TRUE) - min(df$index, na.rm = TRUE))


```


*Recode Approval Questions*

<1> Strongly approve 
<2> Somewhat approve 
<3> Somewhat disapprove 
<4> Strongly disapprove 
<5> Not sure

*I code 5 as NA*

```{r}
df <- df %>%
  mutate(gov_approve = case_when(CC22_320d == 1 ~ 4,
                                 CC22_320d == 2 ~ 3, 
                                 CC22_320d == 3 ~ 2, 
                                 CC22_320d == 4 ~ 1,
                                 CC22_320d == 5 ~ NA_real_)) %>%
  mutate(leg_approve = case_when(CC22_320e == 1 ~ 4,
                                 CC22_320e == 2 ~ 3, 
                                 CC22_320e == 3 ~ 2, 
                                 CC22_320e == 4 ~ 1,
                                 CC22_320e == 5 ~ NA_real_)) %>%
  mutate(house_approve = case_when(CC22_320f == 1 ~ 4,
                                   CC22_320f == 2 ~ 3, 
                                   CC22_320f == 3 ~ 2, 
                                   CC22_320f == 4 ~ 1,
                                   CC22_320f == 5 ~ NA_real_)) %>%
  mutate(sen1_approve = case_when(CC22_320g == 1 ~ 4,
                                  CC22_320g == 2 ~ 3, 
                                  CC22_320g == 3 ~ 2, 
                                  CC22_320g == 4 ~ 1,
                                  CC22_320g == 5 ~ NA_real_)) %>%
  mutate(sen2_approve = case_when(CC22_320h == 1 ~ 4,
                                  CC22_320h == 2 ~ 3, 
                                  CC22_320h == 3 ~ 2, 
                                  CC22_320h == 4 ~ 1,
                                  CC22_320h == 5 ~ NA_real_))
```


*Simple Regressions*

```{r}
df <- df %>%
  filter(
    !is.na(IRR2020),
    !is.na(distrust_federal),
    !is.na(CC22_333b),
    !is.na(birthyr),
    !is.na(educ),
    !is.na(gender4),
    !is.na(race),
    !is.na(commonpostweight),
    !is.na(pid3),
    !is.na(inputstate),
    !is.na(state_rps_support),
    !is.na(trust_federal),
    !is.na(trust_state),
    !is.na(pew_religimp),
    !is.na(gov_approve),
    !is.na(index_01)
  ) %>%
  mutate(
    educ = as.factor(educ),
    gender4 = as.factor(gender4),
    race = as.factor(race),
    party = as.factor(pid3),
    inputstate = as.factor(inputstate),
    religiosity = as.factor(pew_religimp)
  )
```


*Trust in Federal Government*

```{r}
model.1 <- lm(trust_federal ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate + religiosity, data = df, weights = commonpostweight)

summary(model.1)
nobs(model.1)
```

*Trust in State Government*

```{r}
model.2 <- lm(trust_state ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate + religiosity, data = df, weights = commonpostweight)

summary(model.2)
nobs(model.2)
```

*Approval of Governor*

```{r}
model.3 <- lm(gov_approve ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate + 
                religiosity, data = df, weights = commonpostweight)

summary(model.3)
nobs(model.3)
```
*RPS Support*

```{r}
model.4 <- glm(state_rps_support ~ IRR2020 + trust_state+  birthyr + educ + gender4 + race + party + inputstate + 
                religiosity, data = df, weights = commonpostweight, family="binomial")

summary(model.4)
nobs(model.4)
```

```{r}
library(performance)

r2(model.4)
```


*Climate Action Index*
```{r}
model.5 <- lm(index_01 ~ IRR2020+ trust_state + birthyr + educ + gender4 + race + party + inputstate + 
                religiosity, data = df, weights = commonpostweight)

summary(model.5)

nobs(model.5)
```


**MEDIATION ANALYSIS - SIMPLE**

*Does lower trust/approval of government channel an effect on lower approval of RE?*

```{r}
library(mediation)
```


```{r}
model.M <- lm(trust_state ~ IRR2020 + birthyr + educ + gender4 + race + party + religiosity + inputstate, data = df, weights = commonpostweight)

```

```{r}
model.Y <- glm(state_rps_support ~ IRR2020 + trust_state + birthyr + educ + gender4 +race + party + inputstate + religiosity, data = df, weights = commonpostweight, family = "binomial")
```


*Mediator Model*
```{r}
# Mediator model
suppressWarnings(
  med.out <- mediate(model.M, model.Y, treat = "IRR2020", mediator = "trust_state", boot = TRUE, sims = 1000)
)

```

```{r}
summary(med.out)
```


*Alt DV**

```{r}
model.M <- lm(trust_state ~ IRR2020 + birthyr + educ + gender4 + race + party + religiosity + inputstate, data = df, weights = commonpostweight)

```

```{r}
model.Y <- lm(index_01 ~ IRR2020 + trust_state + birthyr + educ + gender4 +race + party + inputstate + religiosity, data = df, weights = commonpostweight)
```

*Mediator Model*
```{r}
# Mediator model
suppressWarnings(
  med.out <- mediate(model.M, model.Y, treat = "IRR2020", mediator = "trust_state", boot = TRUE, sims = 100)
)

```

```{r}
summary(med.out)
```











































```{r}
model.Y <- lm(state_rps_support ~ IRR2020 + trust_state + birthyr + educ + gender4 +race + party + inputstate + religiosity, data = df, weights = commonpostweight)

summary(model.Y)
```


*Mediator Model*

```{r}
# Mediator model
med.out <- mediate(model.M, model.Y, treat = "IRR2020", mediator = "trust_state", boot = TRUE, sims = 1000)

```

```{r}
summary(med.out)

```
















*Recode Spending Preferences*

CC22_443_5	State spending	State legislatures must make choices when making spending decisions on important state programs. How would you like your legislature to spend money on each of the five areas below? transportation/Infrastructure <1> Greatly increase <2> Somewhat increase <3> Maintain <4> Somewhat decrease <5> Greatly decrease

```{r}

df <- df %>%
  mutate(spending_1 = case_when(CC22_443_1 == 1 ~ 5,
                                  CC22_443_1 == 2 ~ 4, 
                                  CC22_443_1 == 3 ~ 3, 
                                  CC22_443_1 == 4 ~ 2,
                                  CC22_443_1 == 5 ~ 1)) %>%
  mutate(spending_2 = case_when(CC22_443_2 == 1 ~ 5,
                                  CC22_443_2 == 2 ~ 4, 
                                  CC22_443_2 == 3 ~ 3, 
                                  CC22_443_2 == 4 ~ 2,
                                  CC22_443_2 == 5 ~ 1)) %>%
  mutate(spending_3 = case_when(CC22_443_3 == 1 ~ 5,
                                  CC22_443_3 == 2 ~ 4, 
                                  CC22_443_3 == 3 ~ 3, 
                                  CC22_443_3 == 4 ~ 2,
                                  CC22_443_3 == 5 ~ 1)) %>%
  mutate(spending_4 = case_when(CC22_443_4 == 1 ~ 5,
                                  CC22_443_4 == 2 ~ 4, 
                                  CC22_443_4 == 3 ~ 3, 
                                  CC22_443_4 == 4 ~ 2,
                                  CC22_443_4 == 5 ~ 1)) %>%
  mutate(spending_5 = case_when(CC22_443_5 == 1 ~ 5,
                                  CC22_443_5 == 2 ~ 4, 
                                  CC22_443_5 == 3 ~ 3, 
                                  CC22_443_5 == 4 ~ 2,
                                  CC22_443_5 == 5 ~ 1)) 


df$spending_avg <- rowMeans(df[, c("spending_1", "spending_2", "spending_3", 
                                   "spending_4", "spending_5")], na.rm = TRUE)



```





*

```{r}
model.4 <- lm(spending_avg ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate + 
                religiosity, data = df, weights = commonpostweight)

summary(model.4)
```





```{r}
model.M <- lm(sen1_approve ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate + religiosity, data = df, weights = commonpostweight)

summary(model.M)
```

```{r}
model.M <- lm(sen2_approve ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate, data = df, weights = commonpostweight)

summary(model.M)
```

```{r}
model.M <- lm(house_approve ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate, data = df, weights = commonpostweight)

summary(model.M)
```





Clean Energy opposition score: 


```{r}

df$ce_opp <- rowMeans(df[, c("CC22_333b", "CC22_350h", "CC22_355c")], na.rm = TRUE)


head(df)
```



```{r}

CC22_333b
CC22_350h
CC22_355c

Do you support or oppose each of the following proposals? Require that each state use a minimum amount of renewable fuels (wind, solar, and hydroelectric) in the generation of electricity even if electricity prices increase a little

CC22_350h	Roll call votes	Over the past two years, Congress voted on many issues. Do you support each of the following proposals? Spend $369 billion for tax credits to encourage the production of solar panels, wind turbines, and batteries; lowers Affordable Care act health deficit by $300 billion by allowing Medicare to negotiate the cost of some prescription drugs and making changes to the tax code


CC22_355c	Executive Orders	For each of the following tell us whether you support or oppose these decisions. Order all federal agencies to buy clean energy, purchase electric vehicles, and make federal building energy efficient

```



```{r}
# Mediator model
model.M <- lm(distrust_federal ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate, data = df, weights = commonpostweight)

# Outcome model
model.Y <- lm(CC22_333b ~ IRR2020 + distrust_federal + birthyr + educ + gender4 +race + party + inputstate, data = df, weights = commonpostweight)

```

```{r}
med.out <- mediate(model.M, model.Y, treat = "IRR2020", mediator = "distrust_federal", boot = TRUE, sims = 1000)

```

```{r}
summary(med.out)
```


```{r}
hist(df$distrust_federal)
```




```{r}
model.M <- glm(distrust_federal ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate,
               data = df,
               family = binomial,
               weights = commonpostweight)

```

```{r}
model.Y <- glm(CC22_333b ~ IRR2020 + distrust_federal + birthyr + educ + gender4 + race + party + inputstate,
               data = df,
               family = binomial,
               weights = commonpostweight)

```

```{r}
library(mediation)

med.out <- mediate(model.M, model.Y,
                   treat = "IRR2020",
                   mediator = "distrust_federal",
                   robustSE = TRUE,
                   boot = TRUE,
                   sims = 1000)

```


```{r}
summary(med.out)

```





```{r}
library(brms)

# Ordered logistic for the mediator
model.M <- brm(distrust_federal ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate,
               data = df,
               family = cumulative("logit"),  # ordinal logistic
               weights = df$commonpostweight,
               chains = 4, cores = 4)

# Binary outcome
model.Y <- brm(CC22_333b ~ IRR2020 + distrust_federal + birthyr + educ + gender4 + race + party + inputstate,
               data = df,
               family = bernoulli(),
               weights = df$commonpostweight,
               chains = 4, cores = 4)

```

```{r}
a <- posterior_samples(model.M, "b_IRR2020")
b <- posterior_samples(model.Y, "b_distrust_federal")
indirect <- a * b
direct <- posterior_samples(model.Y, "b_IRR2020")
total <- indirect + direct

```








**Simple average trust**

```{r}
df$trust_avg <- rowMeans(df[, c("distrust_federal", "distrust_state")], na.rm = TRUE)

```

```{r}
# Mediator model
model.M <- lm(trust_avg ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate, data = df, weights = commonpostweight)

# Outcome model
model.Y <- lm(ce_opp ~ IRR2020 + trust_avg + birthyr + educ + gender4 +race + party + inputstate, data = df, weights = commonpostweight)

```

```{r}
med.out <- mediate(model.M, model.Y, treat = "IRR2020", mediator = "trust_avg", boot = TRUE, sims = 1000)

```

```{r}
summary(med.out)
```


```{r}
hist(df$ce_opp)
```


**Spending Preferences**

```{r}

df$spending_avg <- rowMeans(df[, c("CC22_443_1", "CC22_443_2","CC22_443_3",
                                   "CC22_443_4", "CC22_443_5")], na.rm = TRUE)



```

```{r}
# Mediator model
model.M <- lm(spending_avg ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate, data = df, weights = commonpostweight)

# Outcome model
model.Y <- lm(ce_opp ~ IRR2020 + spending_avg + birthyr + educ + gender4 +race + party + inputstate, data = df, weights = commonpostweight)

```

```{r}
med.out <- mediate(model.M, model.Y, treat = "IRR2020", mediator = "spending_avg", boot = TRUE, sims = 1000)

```

```{r}
summary(med.out)
```




```{r}
mod <- lm(trust_avg ~ IRR2020 + birthyr + educ + gender4 + race + party + inputstate, data = df, weights = commonpostweight)

summary(mod)
```



**Creation of a more complex construciton of a distrust / resentment construct** 

--> also score low on spending preferences?

CC22_443_5	State spending	State legislatures must make choices when making spending decisions on important state programs. How would you like your legislature to spend money on each of the five areas below? transportation/Infrastructure <1> Greatly increase <2> Somewhat increase <3> Maintain <4> Somewhat decrease <5> Greatly decrease


--> low approval of specific institutions? Like congress? 
--> 
CC22_320d	-- Governor approval 


CC22_320g - state senator 1 -- 5 is not sure 
CC22_320h - state senator 2 




BFA: 

LF <- Low trust + low approval + prefers lower spending 

What is the latent trait? 
--> Perception that government projects are city-biased - and do not 
benefit rural areas in the same way 







-------------------------------------


**Only renewable -- or more complex 'climate action' preference construct** 

CC22_333b

Do you support or oppose each of the following proposals? Require that each state use a minimum amount of renewable fuels (wind, solar, and hydroelectric) in the generation of electricity even if electricity prices increase a little


CC22_333	Climate change	From what you know about global climate change or global warming, which of the following statement comes closest to your opinion? <1> Global climate change has been established as a serious problem, and immediate action is necessary <2> There is enough evidence that climate change is taking place and some action should be taken <3> We don't know enough about global climate change, and more research is necessary before we take any actions <4> Concern about global climate change is exaggerated. No action is necessary <5> Global climate change is not occurring; this is not a real issue


CC22_333a	Climate change	Do you support or oppose each of the following proposals? Give the Environmental Protection Agency power to regulate Carbon Dioxide emissions

CC22_333c	Climate change	Do you support or oppose each of the following proposals? Strengthen the Environmental Protection Agency enforcement of the Clean Air Act and Clean Water Act even if it costs U.S. jobs


CC22_333d	Climate change	Do you support or oppose each of the following proposals? Raise the average fuel efficiency for all cars and trucks in the US from 40 miles per gallon to 54.5 miles per gallon by 2025

CC22_333e	Climate change	Do you support or oppose each of the following proposals? Increase fossil fuel production in the U.S. and boost exports of U.S. liquefied natural gas


CC22_350h	Roll call votes	Over the past two years, Congress voted on many issues. Do you support each of the following proposals? Spend $369 billion for tax credits to encourage the production of solar panels, wind turbines, and batteries; lowers Affordable Care act health deficit by $300 billion by allowing Medicare to negotiate the cost of some prescription drugs and making changes to the tax code

CC22_355a	Executive Orders	For each of the following tell us whether you support or oppose these decisions. The United State re-joins the Paris Climate Agreement

CC22_355c	Executive Orders	For each of the following tell us whether you support or oppose these decisions. Order all federal agencies to buy clean energy, purchase electric vehicles, and make federal building energy efficient











pid3 Generally speaking, do you think of yourself as a...? <1> Democrat <2>Republican <3> Independent <4>Other <5> Not sure

pid7 Generally speaking, do you think of yourself as a...? <1> Strong Democrat <2> Not very strong Democrat <3> The Democratic Party <4> Neither <5>The Republican Party <6>Not very strong Republican <7>Strong Republican <8> Not sure <9> Don't know

ideo5 In general, how would you describe your own political viewpoint? <1> Very liberal <2>Liberal <3>Moderate <4>Conservative <5>Very conservative <6> Not sure


CC350	Party ID

Get ruralit meausre

urbancity	Rurality	How would you describe the place where you live? <1> City <2> Suburb <3> Town <4> Rural area <5> Other



```{r}

irr <- vroom("IRR_2000_2020.csv")

test$cty_fips <- as.numeric(test$cty_fips)


test <- left_join(test, irr, by = c("cty_fips" = "FIPS2020"))
```




```{r}
mod <- lm(CC22_333b ~ IRR2020 + inputstate + birthyr + gender4 + race + votereg + pid7, data = test)

summary(mod)
```


Resentment score 


*Low trust*

CC22_423	Trust	How much trust do you have in the federal government in Washington when it comes to handling the nation’s problems? <1> A great deal <2> A fair amount <3> Not very much <4> None at all

CC22_424	Trust	How much trust do you have in the government of the state where you live when it comes to handling the nation’s problems?


State spending -- on specific issues 



```{r}
data_bfa <- df[, c("distrust_federal", "distrust_state")]

# Define variable types: "o" = ordinal, "c" = continuous, "b" = binary
types <- c("o", "o")

fit <- bfa(
  data_bfa,
  num.factor = 1,
  type = types,
  iter = 10000
)

# Posterior mean of the latent trait (trust index)
survey_data$trust_index <- fit$fscores[, 1]

```



```{r}

library(blavaan)

model <- '
  trust =~ distrust_federal + distrust_state
'

fit <- bsem(model, data = data, ordered = c("distrust_federal", "distrust_state"))

scores <- lavPredict(fit)
data$trust_index <- as.numeric(scores)


head(data)

```





CC22_320b	Approval	Do you approve of the way each is doing their job...The U.S. Congress <1> Strongly approve <2> Somewhat approve <3> Somewhat disapprove <4> Strongly disapprove <5> Not sure

until h 



CC22_423
CC22_424
CC22_320b
CC22_320c
CC22_320d
CC22_320e
CC22_320f
CC22_320g
CC22_320h



State spending -- on specific issues 

Racial Resentment 




```{r}
summary(med.out)

```




[CC22_420] {multiple} Would you approve of the use of U.S. military troops in
order to ... ? (Please check all that apply)



